<template>
	<view class="yp" :class="{'theme--Black':CurrentTheme.name==='DarkMode'}">
		<app-headers></app-headers>
		<headers></headers>
		<PullDown ref='PullDown' @onloadStart="onloadStart">
			<crumbs :path="path" @freshFolder="freshFolder"></crumbs>
			<contents ref="contentsss" :FolderList.sync="FolderList" :FileList.sync="FileList" @freshFolder="freshFolder"></contents>
		</PullDown>
	</view>
</template>

<script>
	let CurrentAccout = null; // 当前的使用账户，用户切换用户的判断刷新
	import headers from './header/index.vue';
	import crumbs from '../../common/components/UniCrumbs.vue';
	import contents from './content/index.vue';
	import PullDown from '@/common/components/pulldown.vue';
	import {
		API$GetFolderList
	} from '../../common/API/file.js';
	export default {
		name: '',
		components: {
			headers,
			contents,
			crumbs,
			PullDown
		},
		data() {
			return {
				path: [{
					id: 1,
					text: '洋盘'
				}], // 传入面包屑的路径
				FolderList: [], // 文件夹列表
				FileList: [] // 文件列表
			};
		},
		async created() {
			await this.GetFolderList();
			CurrentAccout = this.$pub.getStorageSync('UserInfo')._id;
		},
		onShow() {
			// #ifdef APP-PLUS
			//设置导航栏前景色
			this.$nextTick(() => plus.navigator.setStatusBarStyle(this.CurrentTheme.name === 'DarkMode' ? 'light' : "dark"))
			// #endif

			uni.showTabBar({
				animation: true
			});

			CurrentAccout = this.$pub.getStorageSync('UserInfo')._id;
			// 切换账户下 更新文件夹 逻辑不同于我的页面 只在账户切换后刷新
			if (CurrentAccout !== this.$pub.getStorageSync('UserInfo')._id) {
				this.freshFolder();
			}
		},
		onLoad() {
			// 面包屑刷新API
			uni.$on('freshFolderAPI', dir => {
				// console.log(dir);
				this.freshFolder(dir);
				return '666'
			});
		},
		onUnload() {
			uni.$off('freshFolderAPI')
		},
		onPullDownRefresh() {
			this.freshFolder();
		},
		// #ifdef APP-PLUS
		onBackPress(e) {
			if (e.from === 'backbutton') {
				if (this.$refs.contentsss.$data.isShow_H5_Mask) { // 添加监听返回键 返回退出文件操作工具栏
					this.$refs.contentsss._data.isShow_H5_Mask = false;
				} else if (this.path.length > 1) {
					this.path.pop();
					let arr = JSON.parse(JSON.stringify(this.path.slice(1)));
					let return_path = '';
					for (let i = 0; arr[i]; i++) {
						return_path += arr[i].text + '/';
					}
					// 重刷文件路径接口
					this.freshFolder(return_path);
				} else {
					uni.showModal({
						title: '提示',
						content: '是否退出洋盘？',
						success: function(res) {
							if (res.confirm) {
								plus.runtime.quit();
							} else if (res.cancel) {
								console.log('用户点击取消');
							}
						}
					});
				}
				return true;
			}
			let main = plus.android.runtimeMainActivity();
			plus.nativeUI.toast = function(str) {
				if (str == '再按一次退出应用') {
					return false;
				}
			};
		},
		// #endif
		methods: {
			// 面包屑刷新API
			freshFolder(dir) {
				// this.path =[{ id: 1, text: '我的阿洋网盘' }]
				// let pathList = path.split('/')
				// let RealyPath = ""  // 处理后传给面包屑的路径
				// for(let i in pathList){
				// 	if(!pathList[i] || i==0) continue
				// 	this.path.push({
				// 		id:new Date().getTime(),
				// 		text:pathList[i]
				// 	})
				// }
				return this.GetFolderList(dir);
			},
			/*********************数据处理***********************************/
			// 注：  不带前缀的 如158920231/5465/asf   只能有5465/asf
			async GetFolderList(dir) {
				let {
					result,
					path,
					msg
				} = await API$GetFolderList({
					dir
				});

				// 处理面包屑的路径
				this.path = [{
					id: 1,
					text: '洋盘'
				}];
				let pathList = path.split('/');
				let RealyPath = ''; // 处理后传给面包屑的路径
				for (let i in pathList) {
					if (!pathList[i] || i == 0) continue;
					this.path.push({
						id: new Date().getTime(),
						text: pathList[i]
					});
				}
				// 处理 文件夹 数据
				this.FolderList = [];
				result.prefixes ?
					result.prefixes.forEach(i => {
						this.FolderList.push({
							checked: false,
							path: i
						});
					}) :
					'';

				// 处理 文件 数据
				this.FileList = [];
				if (result.objects) {
					for (let [inx, item] of Object.entries(result.objects)) {
						item.type = this.$tool.getFileType(item.name);
						if (inx == 0 && item.type == '/') continue;
						this.FileList.push(Object.assign({
							checked: false
						}, item));
					}
				}

				this.$pub.Toast(msg)
				uni.stopPullDownRefresh();
				this.$refs.contentsss.StopFresh()
				return result
			},
			async onloadStart() {
				try {
					await this.freshFolder()
					
				} catch (e) {
				}
				setTimeout(()=>{
					this.$refs.PullDown.closeDownPullLoading()
				},500)
			}
		}
	};
</script>

<style lang="less" scoped>
	.yp {
		/* #ifdef H5 */
		height: calc(100vh - 50px);
		/* #endif */
		/* #ifdef APP-PLUS */
		height: 100vh;
		/* #endif */
		// overflow: scroll;
	}

	// 主题
	.theme--Black {
		@bgcolor: #2b2b2b;
		@activeColor: #000;

		/deep/ .header {
			background-color: #000;
		}

		/deep/ .icon-circle {
			background-color: gray;
			color: #ffffff;
		}

		/deep/ .crumbs {
			background-color: @bgcolor;
		}

		/deep/ .crumbs-item-active {
			background-color: @activeColor;
		}

		/deep/ .theMiddletriangle {
			border-left: 20px solid @activeColor;
		}

		/deep/ .crumbs-item-active::after {
			background-color: @activeColor;
			border-left: 40rpx solid @bgcolor;
		}

		/deep/ .crumbs-item-activeS::after {
			background-color: @bgcolor;
		}
	}
</style>
